package com.qingfeng.medical.controller.admin.infomation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingfeng.currency.dozer.DozerUtils;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.base.ro.EnumsRo;
import com.qingfeng.medical.biz.information.domain.dto.DoctorInfoSaveDTO;
import com.qingfeng.medical.biz.information.domain.dto.DoctorInfoUpdateDTO;
import com.qingfeng.medical.biz.information.domain.entity.DoctorInfo;
import com.qingfeng.medical.biz.information.domain.enums.DoctorPositionEnum;
import com.qingfeng.medical.biz.information.domain.vo.DoctorInfoVo;
import com.qingfeng.medical.biz.information.service.DoctorInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 医师信息表
 *
 * @author 清风学Java
 * @date 2023-04-08 23:43:31
 */
@Api(tags = "医师信息表管理")
@RestController
@RequestMapping("/admin/info/doctorInfo")
@Validated
public class DoctorInfoController extends BaseController {

    @Autowired
    private DoctorInfoService doctorInfoService;
    @Autowired
    private DozerUtils dozerUtils;

    @PreAuthorize("hasAuthority('bnt.doctor.list')")
    @ApiOperation(value = "医师信息表分页列表", notes = "医师信息表分页列表")
    @GetMapping("/list")
    @Log(title = "医师信息表", businessType = BusinessType.SELECT, message = "医师信息表分页列表")
    public Result<DoctorInfoVo> list(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @RequestParam("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @RequestParam("limit") Long limit,
            @ApiParam(name = "username", value = "用户名", required = false)
            @RequestParam("username") String username,
            @ApiParam(name = "position", value = "医师职位", required = false)
            @RequestParam("position") DoctorPositionEnum position) {
        DoctorInfoVo doctorInfoVo = doctorInfoService.findDoctorInfo(page, limit, username, position);
        return Result.ok(doctorInfoVo);
    }

    @PreAuthorize("hasAuthority('bnt.doctor.info')")
    @ApiOperation(value = "医师信息表信息", notes = "医师信息表信息")
    @GetMapping("/info/{id}")
    @Log(title = "医师信息表", businessType = BusinessType.SELECT, message = "医师信息表信息")
    public Result infoById(@PathVariable("id") Long id) {
        DoctorInfo doctorInfo = doctorInfoService.getById(id);
        return Result.ok(doctorInfo);
    }

    @PreAuthorize("hasAuthority('bnt.doctor.info')")
    @ApiOperation(value = "医师信息", notes = "医师信息")
    @GetMapping("/info")
    @Log(title = "医师信息表", businessType = BusinessType.SELECT, message = "医师信息")
    public Result<DoctorInfo> infoDoctor() {
        DoctorInfo doctorInfo = doctorInfoService.getOne(new LambdaQueryWrapper<DoctorInfo>()
                .eq(DoctorInfo::getUserId, getUserId()));
        return Result.ok(doctorInfo);
    }

    @PreAuthorize("hasAuthority('bnt.doctor.update')")
    @ApiOperation(value = "保存医师信息", notes = "保存医师信息")
    @PostMapping("/save")
    @Log(title = "医师信息表", businessType = BusinessType.INSERT, message = "保存医师信息")
    public Result save(@RequestBody @Validated DoctorInfoSaveDTO doctorInfoSaveDTO) {
        DoctorInfo doctorInfo = dozerUtils.map2(doctorInfoSaveDTO, DoctorInfo.class);
        doctorInfo.setUserId(getUserId());
        doctorInfoService.save(doctorInfo);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.doctor.update')")
    @ApiOperation(value = "修改医师信息", notes = "修改医师信息")
    @PutMapping("/update")
    @Log(title = "医师信息表", businessType = BusinessType.UPDATE, message = "修改医师信息")
    public Result update(@RequestBody @Validated DoctorInfoUpdateDTO doctorInfoUpdateDTO) {
        doctorInfoService.updateById(dozerUtils.map2(doctorInfoUpdateDTO, DoctorInfo.class));
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.doctor.refresh')")
    @ApiOperation(value = "同步医师信息", notes = "同步医师信息")
    @GetMapping("/refresh")
    @Log(title = "医师信息管理", businessType = BusinessType.STATUS, message = "同步医师信息")
    public Result refreshInfo() {
        doctorInfoService.refreshDoctorInfo();
        return Result.ok();
    }

    @ApiOperation(value = "返回医师职称枚举", notes = "返回医师职称枚举")
    @GetMapping("/anno/patient")
    @Log(title = "医师信息管理", businessType = BusinessType.SELECT, message = "返回医师职称枚举")
    public Result<List<EnumsRo>> getPatientEnums() {
        return Result.ok(
                Arrays.stream(DoctorPositionEnum.values())
                        .map(p -> EnumsRo.builder()
                                .label(p.getMsg())
                                .value(p.name())
                                .build())
                        .collect(Collectors.toList())
        );
    }
}
